/**  
 * @Title:  CustomActionsController.java   
 * @Package com.platform.controller   
 * @Description: (用一句话描述该文件做什么)   
 * @author: <a href="mailto:15909910367@163.com">吴先生</a> 
 * @date:   2021年4月13日 下午3:44:22   
 * @version V1.0 
 * @Copyright: 2021 吴先生.
 */
package com.platform.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.platform.base.R;
import com.platform.base.mvc.BaseController;
import com.platform.logger.annotation.ApiLog;
import com.platform.service.IDataSource;
import com.platform.utils.IdGen;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**   
 * @ClassName:  CustomActionsController   
 * @Description:自定义操作
 * @author: <a href="mailto:15909910367@163.com">吴先生</a>  
 * @date:   2021年4月13日 下午3:44:22   
 *     
 * @Copyright: 2021 吴元森.  
 */
@Api(tags = "COMMON-自定义数据库操作服务")
@Controller
@RequestMapping("customActions")
public class CustomActionsController extends BaseController{
	
	@Autowired
	private IDataSource dataSourceServices;
	
	
	@ApiOperation("获取数据库表和视图信息-树结构")
	@RequestMapping(value="getDataBaseTableAndView",method= {RequestMethod.GET})
    @ResponseBody
	public Object getDataBaseTableAndView(Long code){
		return dataSourceServices.getDataBaseTableAndView(code);
	}
	
	@ApiOperation("获取数据库表和视图信息-树结构")
	@RequestMapping(value="getDataBaseTableAndViewNoTree",method= {RequestMethod.GET})
    @ResponseBody
	public Object getDataBaseTableAndViewNoTree(Long code){
		return dataSourceServices.getDataBaseTableAndViewNoTree(code);
	}
	
	@ApiOperation("获取数据库表和视图信息-树结构")
	@RequestMapping(value="getDataBaseTableAndViewTree",method= {RequestMethod.GET})
	@ResponseBody
	public Object getDataBaseTableAndViewTree(Long code){
		Map<String,Object> treeMap = Maps.newHashMap();
		treeMap.put("id", IdGen.getUid());
		treeMap.put("text", "资源列表");
		treeMap.put("leaf", false);
		treeMap.put("children", dataSourceServices.getDataBaseTableAndView(code).getData());
		return treeMap;
	}
	
	@ApiOperation("获取表所有字段信息")
	@RequestMapping(value="getTableFields",method= {RequestMethod.GET})
    @ResponseBody 
	public Object getTableFields(Long code,String tableName,HttpServletRequest request, HttpServletResponse response,
            HttpSession session){
		return R.success(dataSourceServices.getFields(tableName, String.valueOf(code)));
	}
	
//	@ApiOperation("分页获取表格数据")
//	@RequestMapping(value="getTableData",method= {RequestMethod.GET})
//    @ResponseBody 
//	public R<?> getTableData(Long dbCode,String tableName,String fields,int start,int limit){
//		ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) SpringContextUtil.getApplicationContext();
//		DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory();
////		tableName = Aes.aesDecrypt(tableName);
////		fields = Aes.aesDecrypt(fields);
//		DataSource ds = dataSourceServices.selectById(dbCode);
//		StringBuilder sqlSb = new StringBuilder();
//		sqlSb.append(" SELECT ").append(fields).append(" FROM ").append(tableName);
//		StringBuilder sqlTotalSb = new StringBuilder();
//		sqlTotalSb.append(" SELECT count(1) as \"TOTAL\" FROM ").append(tableName);
//		javax.sql.DataSource dataSource = (javax.sql.DataSource) beanFactory.getBean(ds.getUseName());
//		try {
//			Connection conn = dataSource.getConnection();
//			String pagingSQL = DialectFactory.getLimitSQLBy(conn.getMetaData(), start, limit, sqlSb.toString());
//			JdbcUtils.closeConnection(conn);
//			
//			SqlMapperAdapter sqlMapperAdapter = new SqlMapperAdapter();
//			sqlMapperAdapter.InjectedSessionFactory((SqlSessionTemplate) SpringContextUtil.getBeanBySqlSessionTemplate(ds.getUseName()));
//
//			Long dataTotal = (Long)sqlMapperAdapter.sqlMapper.selectOne(sqlTotalSb.toString()).getOrDefault("TOTAL", 0);
//			List<Map<String,Object>> datas = sqlMapperAdapter.sqlMapper.selectList(pagingSQL);
//			return R.success(datas, dataTotal.intValue());
//		} catch (SQLException e) {
//			return R.fail(e.getMessage());
//		}
//	}
	
	@ApiOperation("执行SQL语句")
	@ApiLog(value="执行自定义SQL语句", desc = "执行SQL语句")
	@RequestMapping(value="executeSQL",method= {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
	public Object runSQL(Long code,String sql,int start,int limit) {
		return dataSourceServices.runSQL(code, sql,start,limit);
	}
}
